﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using Entity;

namespace Controller
{
    public class QueryController:ControllerBase
    {

        public IList<SalesHistoryEntity> findSalesHistoryByDate(DateTime fromdate, DateTime toDate)
        {
            IList<CustPOItem> custPoItems =(from c in Sdc.CustPOItems
                                                where (c.RequestDate>=fromdate) && (c.RequestDate<=toDate)
                                                select c).ToList();
            IList<SalesHistoryEntity> salesHistoryEntities = new List<SalesHistoryEntity>();
            foreach (var custPoItem in custPoItems)
            {
                SalesHistoryEntity salesHistoryEntity = TransformCustPOItemToSalesHistoryEntity(custPoItem);
                salesHistoryEntities.Add(salesHistoryEntity);
            }
            return salesHistoryEntities;
               

        }
        

        public IList<SalesHistoryEntity> findAllSalesHistory()
        {
            IList<CustPOItem> custPoItems = (from c in Sdc.CustPOItems
                                             select c).ToList();
            IList<SalesHistoryEntity> salesHistoryEntities = new List<SalesHistoryEntity>();
            foreach (var custPoItem in custPoItems)
            {
                SalesHistoryEntity salesHistoryEntity = TransformCustPOItemToSalesHistoryEntity(custPoItem);
                salesHistoryEntities.Add(salesHistoryEntity);
            }
            return salesHistoryEntities;
               
        }

        public IList<PurchaseHistoryEntity> findPurchaseHistoryByDate(DateTime fromdate, DateTime todate)
        {
            IList<VendPOItem> vendPoItems = (from v in Sdc.VendPOItems
                                             where (v.RequestDate >= fromdate) && (v.RequestDate <= todate)
                                             select v).ToList();
            IList<PurchaseHistoryEntity> purchaseHistoryEntities = new List<PurchaseHistoryEntity>();
            foreach (var vendPoItem in vendPoItems)
            {
                PurchaseHistoryEntity purchaseHistoryEntity = TransformVendPOItemToPurchaseHistoryEntity(vendPoItem);
                purchaseHistoryEntities.Add(purchaseHistoryEntity);
            }
            return purchaseHistoryEntities;
        }

        public IList<PurchaseHistoryEntity> findAllPurchaseHistory()
        {
            IList<VendPOItem> vendPoItems = (from v in Sdc.VendPOItems
                                             select v).ToList();
            IList<PurchaseHistoryEntity> purchaseHistoryEntities = new List<PurchaseHistoryEntity>();
            foreach (var vendPoItem in vendPoItems)
            {
                PurchaseHistoryEntity purchaseHistoryEntity = TransformVendPOItemToPurchaseHistoryEntity(vendPoItem);
                purchaseHistoryEntities.Add(purchaseHistoryEntity);
            }
            return purchaseHistoryEntities;
        }
        public static SalesHistoryEntity TransformCustPOItemToSalesHistoryEntity(CustPOItem custPoItem)
        {
            SalesHistoryEntity salesHistoryEntity = new SalesHistoryEntity();
            salesHistoryEntity.ProductNo = custPoItem.Product.No;
            salesHistoryEntity.PoNo = custPoItem.CustPO.No;
            salesHistoryEntity.OrderQty = custPoItem.RequestQty;
            salesHistoryEntity.CompleteQty = custPoItem.CompleteQty;
            salesHistoryEntity.Date = custPoItem.RequestDate.ToString("d MMM, yyyy");
            salesHistoryEntity.CustPoId = custPoItem.CustPOId;
            salesHistoryEntity.CustomerName = custPoItem.CustPO.Customer.ShortName;
            salesHistoryEntity.Status = custPoItem.Status;
            return salesHistoryEntity;

        }
        public static PurchaseHistoryEntity TransformVendPOItemToPurchaseHistoryEntity(VendPOItem vendPoItem)
        {
            PurchaseHistoryEntity purchaseHistoryEntity = new PurchaseHistoryEntity();
            purchaseHistoryEntity.ProductNo = vendPoItem.Product.No;
            purchaseHistoryEntity.PoNo = vendPoItem.VendPO.No;
            purchaseHistoryEntity.OrderQty = vendPoItem.RequestQty;
            purchaseHistoryEntity.CompleteQty = vendPoItem.CompleteQty;
            purchaseHistoryEntity.RequestDate = vendPoItem.RequestDate.ToString("d MMM, yyyy");
            purchaseHistoryEntity.VendorId = vendPoItem.VendPO.Vendor.Id;
            purchaseHistoryEntity.VendorName = vendPoItem.VendPO.Vendor.ShortName;
            purchaseHistoryEntity.VendPoId = vendPoItem.VendPOId;
            purchaseHistoryEntity.Status = vendPoItem.Status;
            
            if (vendPoItem.CompleteDate != null)
            {
                purchaseHistoryEntity.CompleteDate = ((DateTime)vendPoItem.CompleteDate).ToString("d MMM, yyyy");
            }

            //1st, complete date is later than request date
            //2nd, still not complete till now
            if (Convert.ToDateTime(purchaseHistoryEntity.CompleteDate) > Convert.ToDateTime(purchaseHistoryEntity.RequestDate) 
                ||(DateTime.Now > Convert.ToDateTime(purchaseHistoryEntity.RequestDate) && purchaseHistoryEntity.CompleteQty<purchaseHistoryEntity.OrderQty))
            {
                purchaseHistoryEntity.Status = "Late";
            }

            return purchaseHistoryEntity;

        }

        public IList<PurchaseHistoryEntity> findLatePurchaseHistory()
        {
            IList<VendPOItem> vendPoItems = (from v in Sdc.VendPOItems
                                           
                                             select v).ToList();
            IList<PurchaseHistoryEntity> purchaseHistoryEntities = new List<PurchaseHistoryEntity>();
            foreach (VendPOItem vendPoItem in vendPoItems)
            {
                if (vendPoItem.CompleteDate != null && vendPoItem.CompleteDate > vendPoItem.RequestDate)
                {
                    PurchaseHistoryEntity purchaseHistoryEntity = TransformVendPOItemToPurchaseHistoryEntity(vendPoItem);
                    purchaseHistoryEntity.Status = "Late";
                    purchaseHistoryEntities.Add(purchaseHistoryEntity);
                }
            }
            return purchaseHistoryEntities;
        }

        public IList<PurchaseHistoryEntity> FindPurchaseHistoryByPONo(string poNo)
        {
            IList<VendPOItem> vendPoItems = (from v in Sdc.VendPOItems
                                             where v.VendPO.No.ToUpper() == poNo
                                             orderby v.RequestDate descending 
                                             select v).ToList();
            IList<PurchaseHistoryEntity> purchaseHistoryEntities = new List<PurchaseHistoryEntity>();
            foreach (VendPOItem vendPoItem in vendPoItems)
            {
                PurchaseHistoryEntity purchaseHistoryEntity = TransformVendPOItemToPurchaseHistoryEntity(vendPoItem);
                purchaseHistoryEntities.Add(purchaseHistoryEntity);
            }
            return purchaseHistoryEntities;
        }

        public IList<SalesHistoryEntity> FindSalesHistoryByConditions(string poNo, string productNo, DateTime fromDate, DateTime toDate)
        {
            IEnumerable<CustPOItem> custPoItems = Sdc.CustPOItems;

            if (!string.IsNullOrEmpty(poNo))
            {
                custPoItems = (from c in custPoItems
                               where c.CustPO.No.ToUpper() == poNo
                               select c).ToList();
            }

            if (!string.IsNullOrEmpty(productNo))
            {
                custPoItems = (from c in custPoItems
                               where c.Product.No.Contains(productNo) || c.Product.Name.Contains(productNo)
                               select c).ToList();
            }

            if(fromDate!=DateTime.MinValue)
            {
                custPoItems = (from c in custPoItems
                               where c.RequestDate >= fromDate
                               select c).ToList();
            }

            if(toDate!=DateTime.MinValue)
            {
                custPoItems = (from c in custPoItems
                               where c.RequestDate <= toDate
                               select c).ToList();
            }
            custPoItems = (from c in custPoItems
                           orderby c.RequestDate descending
                           select c).ToList();
            IList<SalesHistoryEntity> salesHistoryEntities = new List<SalesHistoryEntity>();
            foreach (CustPOItem custPoItem in custPoItems)
            {
                SalesHistoryEntity salesHistoryEntity = TransformCustPOItemToSalesHistoryEntity(custPoItem);
                salesHistoryEntities.Add(salesHistoryEntity);
            }
            return salesHistoryEntities;
        }

        public IList<SalesHistoryEntity> FindSalesHistoryByPoNo(string poNo, string productNo)
        {
            IEnumerable<CustPOItem> custPoItems = Sdc.CustPOItems;
            
            if(!string.IsNullOrEmpty(poNo))
            {
                custPoItems = (from c in custPoItems
                 where c.CustPO.No.ToUpper() == poNo
                 select c).ToList();
            }

            if(!string.IsNullOrEmpty(productNo))
            {
                custPoItems = (from c in custPoItems
                               where c.Product.No.Contains(productNo) || c.Product.Name.Contains(productNo)
                               select c).ToList();
            }

            custPoItems = (from c in custPoItems
                           orderby c.RequestDate descending 
                           select c).ToList();
            IList<SalesHistoryEntity> salesHistoryEntities=new List<SalesHistoryEntity>();
            foreach (CustPOItem custPoItem in custPoItems)
            {
                SalesHistoryEntity salesHistoryEntity = TransformCustPOItemToSalesHistoryEntity(custPoItem);
                salesHistoryEntities.Add(salesHistoryEntity);
            }
            return salesHistoryEntities;
        }

       

        public IList<PurchaseHistoryEntity> FindPurchaseHistoryByConditions(string productNo, string poNo, int vendorId, DateTime fromDate, DateTime toDate)
        {
            IEnumerable<VendPOItem> vendPoItems = Sdc.VendPOItems;
            if(!string.IsNullOrEmpty(productNo))
            {
                vendPoItems = (from v in vendPoItems
                               where v.Product.No.Contains(productNo) || v.Product.Name.Contains(productNo)
                               select v);
            }
            if(!string.IsNullOrEmpty(poNo))
            {
                vendPoItems = (from v in vendPoItems
                               where v.VendPO.No.Contains(poNo)
                               select v);
            }

            if(vendorId!=0)
            {
                vendPoItems = (from v in vendPoItems
                               where v.VendPO.VendId == vendorId
                               select v);
            }

            if(fromDate!=DateTime.MinValue)
            {
                vendPoItems = (from v in vendPoItems
                          where v.RequestDate >= fromDate
                          select v);
            }

            if(toDate!=DateTime.MinValue)
            {
                vendPoItems = (from v in vendPoItems
                               where v.RequestDate <= toDate
                               select v);
            }
            vendPoItems = (from v in vendPoItems
                           orderby v.RequestDate descending
                           select v);

            IList<PurchaseHistoryEntity> purchaseHistoryEntities = new List<PurchaseHistoryEntity>();
            foreach (VendPOItem vendPOItem in vendPoItems)
            {
                PurchaseHistoryEntity purchaseHistoryEntity =
                    TransformVendPOItemToPurchaseHistoryEntity(vendPOItem);
                purchaseHistoryEntities.Add(purchaseHistoryEntity);
            }
            return purchaseHistoryEntities;
        }
    }
}
